package com.homework.phase1.module4;

import java.io.*;

public class ThreadRuner implements Runnable{

    private File file;
    private File file1;

    public ThreadRuner(File file, File file1) {
        this.file = file;
        this.file1 = file1;
    }

    @Override
    public void run() {
        copyAll(file,file1);

    }

    public void copyAll(File file,File file1)  {

        //获取源目录下所有的内容
        File[] filesArray = file.listFiles();

        //遍历源目录下所有内容
        for(File f : filesArray){
            //获取源目录下内容名称
            String name = f.getName();

            //如果该目录下仍然有文件夹
            if(f.isDirectory()){

                //创建file类，将目标根目录与源目录文件夹名称拼接，获得目标路径
                File f1 = new File(file1+"\\"+name);
                System.out.println(file1+"\\"+name);
                //通过目标路径进行创建文件夹
                f1.mkdir();
                //向内遍历
                copyAll(f,f1);
            }
            //如果该目录下有文件，则通过创建io流对文件进行复制
            if(f.isFile()){
                BufferedReader br = null;
                BufferedWriter bw = null;
                try {
                    br = new BufferedReader(new FileReader(f.getAbsolutePath()));
                    bw = new BufferedWriter(new FileWriter(file1+"\\"+name));

                    String s= null;
                    while ((s = br.readLine()) != null) {
                        bw.write(s);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (null != bw) {
                        try {
                            bw.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (null != br) {
                        try {
                            br.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                //File f1 = new File(file1+"\\"+name);
                System.out.println(file1+"\\"+name);


            }

        }
    }
}
